home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / prodpack.zip / DB4PPSRC.EXE / _TMPNAME.PRG < prev    next >
Text File  |  1993-05-04  |  4KB  |  141 lines

  1. FUNCTION _TmpName    && Returns a pseudo-random file root name
  2. PARAMETER pc_ext
  3. *--------------------------------------------------------------------
  4. * NAME
  5. *   _TmpName - Returns a pseudo-random file root name.
  6. *
  7. * SYNOPSIS
  8. *   _TmpName( [.ext] )
  9. *
  10. * DESCRIPTION
  11. *   _TMPNAME() returns an pseudo-random string of
  12. *   digits suitable for use as a temporary file name.
  13. *   Eight digits (sometimes fewer) are returned.
  14. *   Successive calls to _TMPNAME() can be used to
  15. *   generate a series of unique file names.
  16. *
  17. *   An optional file extension can be passed as an
  18. *   argument.  If this is done, _TMPNAME will make
  19. *   sure that the file name it returns does not already
  20. *   exist within the current dBASE path setting.
  21. *
  22. *   If either the DBTMP or TMP DOS environment variables
  23. *   are set, _TMPNAME() will use its value for a path
  24. *   prefix.  If both are set, the DBTMP value is used.
  25. *
  26. * PARAMETERS
  27. *   pc_ext - optional file name extension.  May optionally
  28. *   start with a ".", followed by up to three characters.
  29. *   (Note that some characters are not allowed in file names,
  30. *    depending on the specific operating system in use.)
  31. *
  32. * EXAMPLE
  33. *
  34. *   lc_tmpfile = _TMPNAME(".TMP")
  35. *   * Possible return value: "87113336.TMP"
  36. *   USE master
  37. *   COPY TO (lc_tmpfile)
  38. *   * The file "87113336.TMP" would now exist
  39. *
  40. * LIMITATIONS
  41. *   If _TMPNAME() is used without the extension
  42. *   parameter, the FILE() function can be used to
  43. *   make certain that a created file name does not
  44. *   already exist.
  45. *
  46. *   _TMPNAME() assumes the extension argument has
  47. *   only characters allowed in filenames.
  48. *
  49. *   Note also that leading 0's will not be returned.
  50. *   If you desire exactly eight digits, this line:
  51. *     TRANSFORM( RAND(-1) * 100000000, "@L 99999999" )
  52. *   returns a random string of digits that is always
  53. *   eight characters long.
  54. *
  55. * SEE ALSO:
  56. *   RAND(), FILE()
  57. *
  58. *--------------------------------------------------------------------
  59.  
  60.   PRIVATE lc_env, lc_ext, lc_prefix, lc_root, lc_slash, ;
  61.           ll_err, lh_chkit
  62.  
  63.   IF LEFT( OS(), 3 ) = "DOS"
  64.     lc_slash = "\"
  65.   ELSE
  66.     lc_slash = "/"
  67.   ENDIF
  68.  
  69.   lh_chkit = 0
  70.  
  71.   lc_env = GETENV( "DBTMP" )
  72.   IF .NOT. ISBLANK( lc_env )
  73.     ll_err = .F.
  74.     ON ERROR ll_err = .T.
  75.     lc_prefix = IIF( RIGHT( lc_env, 1 ) = lc_slash, lc_env, lc_env + lc_slash )
  76.     lh_chkit = FCREATE( lc_prefix + [CHECKIT.OUT] )
  77.     IF lh_chkit > 0
  78.       IF FCLOSE( lh_chkit )
  79.         ERASE ( lc_prefix + [CHECKIT.OUT] )
  80.       ENDIF
  81.     ELSE
  82.       lc_env = ""
  83.       ll_err = .F.
  84.     ENDIF
  85.     ON ERROR
  86.   ENDIF
  87.  
  88.   IF ISBLANK( m->lc_env )
  89.     lc_env = GETENV( "TMP" )
  90.     IF .NOT. ISBLANK( lc_env )
  91.       ll_err = .F.
  92.       ON ERROR ll_err = .T.
  93.       lc_prefix = IIF( RIGHT( lc_env, 1 ) = lc_slash, lc_env, lc_env + lc_slash )
  94.       lh_chkit = FCREATE( lc_prefix + [CHECKIT.OUT] )
  95.       IF lh_chkit > 0
  96.         IF FCLOSE( lh_chkit )
  97.           ERASE ( lc_prefix + [CHECKIT.OUT] )
  98.         ENDIF
  99.       ELSE
  100.         lc_env = ""
  101.         ll_err = .F.
  102.       ENDIF
  103.       ON ERROR
  104.     ENDIF
  105.  
  106.     IF ISBLANK( m->lc_env )
  107.       lc_prefix = ""
  108.     ELSE
  109.       lc_prefix = IIF( RIGHT( lc_env, 1 ) = lc_slash, lc_env, lc_env + lc_slash )
  110.     ENDIF
  111.  
  112.   ELSE
  113.     lc_prefix = IIF( RIGHT( lc_env, 1 ) = lc_slash, lc_env, lc_env + lc_slash )
  114.   ENDIF
  115.  
  116.   IF PCOUNT() = 0
  117.     lc_root = m->lc_prefix + LTRIM( STR( RAND( -1 ) * 100000000, 8 ) )
  118.     RETURN( lc_root )
  119.   ELSE
  120.  
  121.     IF .NOT. "." $ m->pc_ext
  122.       lc_ext = "." + m->pc_ext
  123.     ELSE
  124.       lc_ext = m->pc_ext
  125.     ENDIF
  126.  
  127.     lc_ext = SUBSTR(m->lc_ext, 1, 4)
  128.  
  129.     DO WHILE .T.
  130.       lc_root = LTRIM( STR( RAND( -1 ) * 100000000, 8 ) )
  131.  
  132.       IF .NOT. FILE( m->lc_prefix + m->lc_root + m->lc_ext )
  133.         RETURN( m->lc_prefix + m->lc_root + m->lc_ext )
  134.       ENDIF
  135.  
  136.     ENDDO
  137.  
  138.   ENDIF
  139. *-- EOF: _TmpName( [.ext] )
  140.  
  141.